<template>
  <div class="login">
    <div class="form-box">
      <!-- 头框 -->
      <div class="project-pic"><img src="@/assets/uploads/logo.png" /></div>
      <!-- 头框 -->

      <el-form
        :model="form"
        ref="ruleForm2"
        status-icon
        :rules="rules2"
        class="demo-ruleForm"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <!-- 用户名 -->

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="form.password"
            autocomplete="off"
            prefix-icon="iconfont icon-mima"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->

        <!-- 按钮 -->
        <el-form-item>
          <el-button plain type="primary" @click="submitForm('ruleForm2')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm2')">重置</el-button>
        </el-form-item>
        <!-- 按钮 -->
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      }
      callback();
    };
    return {
      form: {
        username: "admin",
        password: "123456",
      },
      rules2: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [{ validator: validatePass, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      console.log(this.$refs[formName]);
      this.$refs[formName].validate(async (validity) => {
        if (!validity) return false;
        let { data: responseData } = await this.$http.post("/login", this.form);
        // if (data.meta.status !== 200) return console.log("登录失败");
        // console.log("登录成功");

        let status = responseData.meta.status != 200;
        let msg = responseData.meta.msg;

        this.$message({
          message: status ? `登录失败: ${msg}` : `${msg}`,
          type: status ? "warning" : "success",
        });
        /*
          1、将登录成功之后的token，保存到客户端的sessionStorage中。
            1.1、项目中除了登录之外的其他API接口，必须在登录之后才能访问。
                (使用sessionStorage存储登录token信息来判断是否登陆过)
            1.2、token只应在当前网站打开期间生效，所以将token保存在sessionStorage中。
        */
        window.sessionStorage.setItem("token", responseData.data.token);
        //  2、通过编程式导航跳转到主页，路由地址是/home
        this.$router.push({
          path: "/home",
        });
      });
      console.log("提交");
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      console.log("重置");
    },
  },
};
</script>

<style scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #00374e;
}
.form-box {
  width: 350px;
  padding: 20px;
  border-radius: 4px;
  box-shadow: 0 0 5px rgb(0 0 0 / 0.2);
  background-color: #fff;
}
.project-pic {
  width: 150px;
  height: 150px;
  border-radius: 50%;
  margin: 0 auto;
  overflow: hidden;
  border: 10px solid #fff;
  margin-top: -25%;
  background-color: #fff;
  box-shadow: 0 0 5px rgb(0 0 0 / 0.2);
  margin-bottom: 22px;
}
.project-pic img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: 0 10px;
  background-color: #efefef;
}
.el-form-item:last-child {
  text-align: center;
  margin-bottom: 0;
}
</style>